/*
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-10-12 15:23:07
 * @LastEditTime : 2020-02-05 16:22:16
 * @LastEditors  : lijing
 */
import axios from '../../request/http'
import url from '../../request/url'
const articles = {
  state: {
    data: null,
    totalPages: 0,
    currentPage: 1,
    totalCount: 0,

    query: {
      condition: null, // str category tag
      data: null,
    },
  },
  mutations: {
    setPageInfo(state, pageInfo) {
      state.data = pageInfo.data;
      state.totalPages = pageInfo.totalPages;
      state.totalCount = pageInfo.totalCount;
      state.currentPage = pageInfo.currentPage;
    },
    setQueryString(state, str) {
      state.query.str = str;
    },
    setCurrentPage(state, pageNumber) {
      state.currentPage = pageNumber;
    },
    // 这里需要注意的是，mutations传递的参数只能是一个，传递多个值的时候，只有第一个参数有值，其他参数全是null
    setCondition(state, params) {
      state.query = params;
    }
  },
  actions: {
    getArticles(context) {
      let query = context.state.query;
      axios.get(url.comm.pageArticles, {
        params: {
          pageNumber: context.state.currentPage,
          condition: query.condition,
          data: query.data,
        }
      }).then(res => {
        context.commit('setPageInfo', res.data);
      });
    }
  }
}
export default articles;